package com.lifang.imgsoa.qiniu;

import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

public class TokenFactory
{
    private TokenFactory()
    {

    }

    private final String ACCESS_KEY = "Ng7_hQYnRgO6nD5iVwFnVmaOeBDqFodzUwiJ9p7P";
    private final String SECRET_KEY = "DJXg6lzdHVQUlp5hfnmNJKR828RvW06JpPuJtsnd";
    private final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    
    private static TokenFactory tokenFactory;
    
    public static TokenFactory initFactory()
    {
        if(tokenFactory == null)
        {
            return new TokenFactory();
        }
        else
        {
            return tokenFactory;
        }
    }

    public String getUpToken()
    {
        return auth
                .uploadToken("huigou", null, 3600, new StringMap().putNotEmpty("returnBody", "{\"key\": $(key), \"hash\": $(etag), \"width\": $(imageInfo.width), \"height\": $(imageInfo.height)}"));
    }

    // 简单上传，使用默认策略
    public String getUpToken0()
    {
        return auth.uploadToken("huigou");
    }

    // 覆盖上传
    public String getUpToken1(String key)
    {
        return auth.uploadToken("huigou", key);
    }

    // 设置指定上传策略
    public String getUpToken2()
    {
        return auth.uploadToken("huigou", null, 3600, new StringMap().put("callbackUrl", "call back url").putNotEmpty("callbackHost", "").put("callbackBody", "key=$(key)&hash=$(etag)"));
    }

    // 设置预处理、去除非限定的策略字段
    public String getUpToken3()
    {
        return auth.uploadToken("huigou", null, 3600, new StringMap().putNotEmpty("persistentOps", "").putNotEmpty("persistentNotifyUrl", "").putNotEmpty("persistentPipeline", ""), true);
    }
}
